#!/usr/bin/env ruby
# encoding: binary

require '../common'
require './GTXcommon'

# monkey patching to ensure forward compatibility with Ruby > 1.9
if String.instance_methods.include?(:encoding)
  class Array
    alias _pack pack # the person who wrote this piece of sh*t is an idiot
    def pack(*argv) # I didn't ask you to convert it to UTF-8! Don't presume it!
      _pack(*argv).force_encoding('ASCII-8BIT')
    end
  end
end

$cmap = {} # char map
loadCharMap() do |key, val|
  c = nil
  unless MASKHIRAGANA # hiragana (ignore if MASKHIRAGANA is set true)
    c = (key & 0xff) - 0x10 if (0x2420..0x2474).include?(key)
  end
  c = (key & 0xff) + 0x45 if (0x2520..0x257a).include?(key) # katakana
  c = (key & 0xff) + 0xa0 if (0x2120..0x212f).include?(key) # punctuations
  if c
    c = c.chr
  else
    c = (key + (OFFSET << 8)) & 0xffff # this is a 16-bit short
    c = [c].pack('n') # big-endian
  end
  $cmap[[val].pack('U')] = c
end

$cmap["|\r\n"] = "\0" # one paragraph ends
$cmap[";\r\n"] = "\1" # line break

TXTfName = $*[0]
unless TXTfName
  puts("Usage: encodeGTX [-p] [-o <offset>] <TXT filename>\n-y     \tSuppress confirming prompts on warning messages.\n-p     \tIndicates that `MTE.exe` has been patched to be able to display more Chinese (Kanji) chars, so 2-byte hiragana chars will not be encoded into 1-byte, and the default offset will be changed from 0xB0 to 0xF0.\n-o 0xNN\tIf the offset is not the default value (0xB0 or 0xF0), it can be assigned here. It must be an 8-bit unsigned int (0x00 to 0xFF).\n\n<path>\tThe TXT plain text file to be encoded. The encoded GTX/CTX file will be saved as <path>.GTX/CTX. Especially, note that\n\t1) The text file must be in UTF-8 encoding;\n\t2) It must not contain chars that are not present in the Shift-JIS code table (you can refer to https://uic.jp/charset/show/cp932);\n\t3) It must use `\\r\\n` (not `\\n`) as line breaks (EOL);\n\t4) Before EOL of each line, there must be either a semicolon (;) to indicate line break or a vertical slash (|) to indicate end of a paragraph.\n\tYou can refer to any TXT file generated by `decodeGTX` as a good example.\n\nIf any 3-byte char groups in the TXT file could not be encoded, the prompt will be 'Conversion incomplete (... X retained)' where X > 0, and you will be able to see the retained char groups in the form of [0xLLNNMM={<UTF-8>}] in the output GTX/CTX file.")
  pauseExit
end

GTXfName = dropExt(TXTfName) + (MASKHIRAGANA ? '.CTX' : '.GTX')
if File.exist?(GTXfName)
  puts "Warning: #{GTXfName} already exists. If you choose to continue, the file will be overwritten! "
  pause unless YES
end

o = open(GTXfName, 'wb')
f = open(TXTfName, 'rb')
succ = fail = 0
while !f.eof?
  byte3 = f.read(3)
  c = $cmap[byte3]
  if c
    o.write(c); succ += 1
  else
    o.write("[0x#{byte3.unpack('H*')[0]}={#{byte3}}]"); fail += 1
  end
end
o.close
f.close
puts "Conversion #{fail.zero? ? 'OK' : 'incomplete'} (#{succ} converted; #{fail} retained)."
pauseExit
